package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> leverAll() {
        List<CategoryEntity> ceList = baseMapper.selectList(null);
        //过滤获取一级标签
        List<CategoryEntity> collect = ceList.stream()
                                        .filter(s -> s.getParentCid() == 0)
                                        .filter(s -> s.getShowStatus() == 0)
                                        .map((c) -> {
                                            c.setChildren(selectChildern(c,ceList));
                                            return c;
                                        })
                                        .collect(Collectors.toList());
        return collect;
    }

    //递归获取所有标签
    List<CategoryEntity> selectChildern(CategoryEntity root,List<CategoryEntity> all){
         List<CategoryEntity> collect = all.stream()
                 .filter(a -> root.getCatId() == a.getParentCid())
                 .filter(a -> a.getShowStatus() == 0)
                 .map((c) -> {
                    c.setChildren(selectChildern(c, all));
                    return c;
                })
                .collect(Collectors.toList());
        return  collect;
    }


    @Override
    public int deleteList(CategoryEntity categoryEntity) {
        List<Long> i = new ArrayList<>();
        if(categoryEntity.getChildren().size()>0){
            i = getChildrenId(categoryEntity.getChildren());
        }
        System.out.println(i.toString());
        i.add(categoryEntity.getCatId());
        int i1 = categoryDao.deleteList(i);
        System.out.println(i1);
        return 1;
    }

    @Override
    public List<Long> findcatelogPath(Long catelongId) {
        List<Long> path = new ArrayList<>();
        findPath(path,catelongId);
        return path;
    }
    private void findPath(List<Long> path,Long catelongId){
        CategoryEntity categoryEntity = this.getById(catelongId);
        if(categoryEntity.getParentCid() != 0)
            findPath(path,categoryEntity.getParentCid());
        path.add(categoryEntity.getCatId());
    }

    //递归获取所有下级id
    List<Long> getChildrenId(List<CategoryEntity> list){
        List<Long> is = new ArrayList<>();
        for (CategoryEntity categoryEntity : list) {
            if(categoryEntity.getChildren() != null){
                List<Long> childrenId = getChildrenId(categoryEntity.getChildren());
                for (Long aLong : childrenId) {
                    is.add(aLong);
                }
            }
            is.add(categoryEntity.getCatId());
        }
        return is;
    }

}